﻿using System.ComponentModel;
using System.Windows.Forms;
using Slippor.Common.PluginFramework;
using Slippor.Douban.Radio.Tracker;
using System;

namespace LyricTrackerPlugin
{
    [Description("获取并显示当前歌曲的歌词。")]
    [DisplayName("歌词插件")]
    [Author("拖鞋不脱")]
    [Version(typeof(LyricTrackerVersion), LyricTrackerPlugin.Version)]
    [PluginCategory(PluginCategory.Other)]
    [Config(typeof(LyricTrackerConfig))]
    public class LyricTrackerPlugin : TrackerPlugin
    {
        public const String Version = "1.0.0.0";
        private static readonly LyricTracker _tracker = new LyricTracker();
        protected override ITracker Tracker
        {
            get { return _tracker; }
        }

        private const int _command = 10888;
        internal void RegHotkey()
        {
            var hotkeyService = (IHotkeyService)_application.GetService(typeof(IHotkeyService));
            if (hotkeyService != null)
            {
                hotkeyService.RegHotkey(_command, _tracker);
            }
        }

        internal void UnRegHotkey()
        {
            var hotkeyService = (IHotkeyService)_application.GetService(typeof(IHotkeyService));
            if (hotkeyService != null)
            {
                hotkeyService.RemoveHotkey(_command);
            }
        }

        private readonly ToolStripMenuItem toolStripItem = new ToolStripMenuItem("显示歌词")
        {
            AutoSize = true,
            DisplayStyle = ToolStripItemDisplayStyle.Text
        };

        public LyricTrackerPlugin()
        {
            toolStripItem.Click += (send, e) =>
                {
                    ((LyricTracker) _tracker).ShowLyric();
                };
            Loaded += new System.EventHandler(LyricTrackerPlugin_Loaded);
            UnLoaded += new System.EventHandler(LyricTrackerPlugin_UnLoaded);
        }

        private void LyricTrackerPlugin_UnLoaded(object sender, System.EventArgs e)
        {
            _application.RemoveToolStripItem(toolStripItem);
            _tracker.Stop();
        }

        private void LyricTrackerPlugin_Loaded(object sender, System.EventArgs e)
        {
            _application.AddToolStripItem(toolStripItem);
            if (_tracker.IsShow)
            {
                _tracker.ShowLyric();
            }
        }
    }
}